var _hmt = _hmt || [];
(function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?6fcd62ad8d4fa46fe6c001c4876b8e3d";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s)
})();
(function(para) {
    var p = para.sdk_url, n = para.name, w = window, d = document, s = 'script',x = null,y = null;
    w['sensorsDataAnalytic201505'] = n;
    w[n] = w[n] || function(a) {return function() {(w[n]._q = w[n]._q || []).push([a, arguments]);}};
    var ifs = ['track','quick','register','registerPage','registerOnce','trackSignup', 'trackAbtest', 'setProfile','setOnceProfile','appendProfile', 'incrementProfile', 'deleteProfile', 'unsetProfile', 'identify','login','logout','trackLink','clearAllRegister','getAppStatus'];
    for (var i = 0; i < ifs.length; i++) {
      w[n][ifs[i]] = w[n].call(null, ifs[i]);
    }
    if (!w[n]._t) {
      x = d.createElement(s), y = d.getElementsByTagName(s)[0];
      x.async = 1;
      x.src = p;
      x.setAttribute('charset','UTF-8');
      y.parentNode.insertBefore(x, y);
      w[n].para = para;
    }
  })({
    sdk_url: '//cdn.followme.com/analytics/sensors-1.9.1/sensorsdata.min.js',
    heatmap_url: '//cdn.followme.com/analytics/sensors-1.9.1/heatmap.min.js',
    name: 'sa',
    web_url: 'https://analytics.followme.com/?project=followme',
    server_url: 'https://analytics.followme.com/sa?project=followme',
    heatmap:{},
    show_log:false
  });
  sa.quick('autoTrack');

(function() {
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }
    try {
        var vcode = getQueryString('vcode');
        if (vcode) {
            sa.setProfile({ vcode: vcode });
        }
        var from = getQueryString('from');
        if (from) {
            sa.setProfile({ from: from });
        }
    } catch (e) { console.log(e); }
})();
window.FMAnalytics = (function() {
    return {
        login: function(USERID) {
            if (!USERID || isNaN(USERID)) {
                return;
            }

            var time = (new Date()).getTime();
            USERID = Number(USERID);
            try {
                if (window.sa) {
                    window.sa.login(USERID);
                    window.sa.track('weblogin', { userId: USERID, loginTime: time });
                }
            } catch (e) {
                setTimeout(function() {
                    if (window.sa) {
                        window.sa.login(USERID);
                        window.sa.track('weblogin', { userId: USERID, loginTime: time });
                    }
                }, 3000);
            }

            try {
                _hmt.push(['_trackEvent', 'login', 'login', 'login', USERID + ''])
            } catch (e) {
                setTimeout(function() {
                    _hmt.push(['_trackEvent', 'login', 'login', 'login', USERID + '']);
                }, 3000);
            }
        },
        register: function(params) {
            var USERID = params.userId; //这里的userid是你登录或者注册成功之后返回的用户id，此id也可以写入到localStorage, 至于读取就是引入的项目处理
            if (!USERID || isNaN(USERID)) {
                return;
            }
            
            USERID = Number(USERID);
            var time = (new Date()).getTime();

            var param = {
                userId: USERID, //用户id
                channel: params.channel || 'fm', //渠道，默认是fm，有qq, weixin, weibo这几种
                type: params.type || 'mobile', //注册方式，有email和mobile
                mobile: params.mobile || '', //如果注册方式是mobile，这个就是注册的号码
                email: params.email || '', //如果注册方式是email，这个就是邮箱,
                platform: params.platform || 'pc.web', //平台：目前有m.web和pc.web
                registerTime: time, //记录时间,
                vcode: params.vcode || window.localStorage.vcode, //vcode
                href: params.href || window.location.href,
                Nickname: params.nickname || ''
            };

            try {
                if (window.sa) {
                    window.sa.login(USERID);
                    window.sa.track('webregister', param);
                    if(params.nickname){
                        sa.setProfile({ Nickname: params.nickname });    
                    }
                }
            } catch (e) {
                setTimeout(function() {
                    window.sa.login(USERID);
                    window.sa.track('webregister', param);
                }, 3000);
            }

            var opt = JSON.stringify(param);
            
            try {
                _hmt.push(['_trackEvent', 'reg', 'reg', param.userId]);
            } catch (e) {
                setTimeout(function() {
                    _hmt.push(['_trackEvent', 'reg', 'reg', param.userId]);
                }, 3000);
            }
        }
    }
})();
